package com.ruoyi.project.system.controller;

import java.util.List;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.domain.SysType;
import com.ruoyi.project.system.service.ISysTypeService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;

/*
 * 分类Controller
 *
 * @author jjp
 * @date 2020-04-09
 */
@RestController
@RequestMapping("/system/type")
public class SysTypeController extends BaseController {
    @Autowired
    private ISysTypeService sysTypeService;

    /**
     * 查询分类列表
     */
    @PreAuthorize("@ss.hasPermi('system:type:list')")
    @GetMapping("/list")
    public AjaxResult list(SysType sysType) {

        List<SysType> types = sysTypeService.selectSysTypeList(sysType);
        return AjaxResult.success(sysTypeService.buildTypeTree(types));
    }

    /**
     * 获取分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:type:query')")
    @GetMapping(value = "/{typeId}")
    public AjaxResult getInfo(@PathVariable("typeId") Long typeId) {
        return AjaxResult.success(sysTypeService.selectSysTypeById(typeId));
    }

    /**
     * 新增分类
     */
    @PreAuthorize("@ss.hasPermi('system:type:add')")
    @Log(title = "分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysType sysType) {
        return toAjax(sysTypeService.insertSysType(sysType));
    }

    /**
     * 修改分类
     */
    @PreAuthorize("@ss.hasPermi('system:type:edit')")
    @Log(title = "分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysType sysType) {

        return toAjax(sysTypeService.updateSysType(sysType));
    }

    /**
     * 删除分类
     */
    @PreAuthorize("@ss.hasPermi('system:type:remove')")
    @Log(title = "分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{typeIds}")
    public AjaxResult remove(@PathVariable Long[] typeIds) {
        return toAjax(sysTypeService.deleteSysTypeByIds(typeIds));
    }

    /**
     * 查询服务详细类型
     * */
    @PostMapping("/queryDetail")
    public AjaxResult queryDetail(@RequestBody SysType sysType){
        return AjaxResult.success(sysTypeService.queryDetail(sysType.getTypeId()));
    }
}
